package com.prp.gwt.client.register;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.prp.gwt.client.ClientUtility;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.vo.User;
import com.prp.gwt.client.widgets.register.RegisterForm;
import com.prp.gwt.client.widgets.register.RegisterView;

public class RegisterClientListener{

	private static RegisterClientListener instance;
	private RegisterServiceAsync service = GWT.create(RegisterService.class);

	public static RegisterClientListener getInstance() {
		if (instance == null)
			instance = new RegisterClientListener();
		return instance;
	}
	
	public void register(User user, String password) {
		service.register(user, password, Host.getHOST(), new AsyncCallback<Map<Integer, String>>() {

			@Override
			public void onFailure(Throwable caught) {
				ClientUtility.handleException(caught);
			}

			@Override
			public void onSuccess(Map<Integer, String> result) {
				if (result.isEmpty()) {
					RegisterView.getInstance().showWidget(1);
				} else
					RegisterForm.getInstance().showErr(result);
			}

		});
	}

}
